/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nivelacesso.bd;

import conexao.ConexaoSingleton;
import conexao.DAOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import nivelacesso.classes.Nivel;

/**
 *
 * @author Wolverine
 */
public class NivelDao implements INivelDao{
    EntityManager manager;

    public NivelDao() throws DAOException {
        try {
            this.manager = ConexaoSingleton.getConexao();
        } catch (Exception ex) {
            throw new DAOException("Erro na conexão com Banco de Dados", ex);
        }
    }
    
    public void adcionarNivel(Nivel nivel) throws DAOException{
        try{
            manager.getTransaction().begin();
            manager.persist(nivel);
            manager.getTransaction().commit();
          }catch(Exception ex){
              if (manager.getTransaction().isActive()){
                  manager.getTransaction().rollback();
              }
              throw new DAOException("Erro ao Salvar no Banco de Dados", ex);
          }
    }

    public void excluirNivel(Nivel nivel) throws DAOException {
        try{
            manager.getTransaction().begin();
            manager.remove(nivel);
            manager.getTransaction().commit();
          }catch(Exception ex){
              if (manager.getTransaction().isActive()){
                  manager.getTransaction().rollback();
              }
              throw new DAOException("Erro ao Salvar no Banco de Dados", ex);
          }
    }

    
    public List<Nivel> consultarNivel() throws DAOException {
        try{
            Query resultado = manager.createQuery("Select n from Nivel n");
            return resultado.getResultList();
        }catch(Exception ex){
            throw new DAOException("Erro de consulta", ex);
        }
    }



}
